package service;

import dao.*;
import model.*;

import java.util.Date;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class PropertyService {
    private OwnerDao ownerDao = new OwnerDao();
    private HouseDao houseDao = new HouseDao();
    private PropertyFeeDao propertyFeeDao = new PropertyFeeDao();
    private ParkingSpaceDao parkingSpaceDao = new ParkingSpaceDao();

    // 业主CRUD
    public void addOwner(Owner owner) throws Exception {
        ownerDao.addOwner(owner);
    }

    public void updateOwner(Owner owner) throws Exception {
        ownerDao.updateOwner(owner);
    }

    public void deleteOwner(int id) throws Exception {
        ownerDao.deleteOwner(id);
    }

    public Owner getOwnerById(int id) throws Exception {
        return ownerDao.getOwnerById(id);
    }

    public List<Owner> getAllOwners() throws Exception {
        return ownerDao.getAllOwners();
    }

    // 房屋权限管理
    public void addOwnerToHouse(int ownerId, String houseId) throws Exception {
        if (ownerDao.getOwnerById(ownerId) == null || houseDao.getHouseById(houseId) == null) {
            System.out.println("业主或房屋不存在");
            return;
        }
        Connection conn = dao.DBUtil.getConnection();
        String sql = "INSERT INTO own (ownerId, houseId) VALUES (?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, ownerId);
        ps.setString(2, houseId);
        ps.executeUpdate();
        ps.close();
        conn.close();
        System.out.println("添加成功");
    }

    public void removeOwnerFromHouse(int ownerId, String houseId) throws Exception {
        Connection conn = dao.DBUtil.getConnection();
        String checkSql = "SELECT 1 FROM own WHERE ownerId=? AND houseId=?";
        PreparedStatement checkPs = conn.prepareStatement(checkSql);
        checkPs.setInt(1, ownerId);
        checkPs.setString(2, houseId);
        java.sql.ResultSet rs = checkPs.executeQuery();
        boolean exists = rs.next();
        rs.close();
        checkPs.close();
        if (exists) {
            String sql = "DELETE FROM own WHERE ownerId=? AND houseId=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, ownerId);
            ps.setString(2, houseId);
            ps.executeUpdate();
            ps.close();
            conn.close();
            System.out.println("删除成功");
        } else {
            conn.close();
            System.out.println("记录不存在");
        }
    }

    public List<String> getHouseIdsByOwnerId(int ownerId) throws Exception {
        return ownerDao.getHouseIdsByOwnerId(ownerId);
    }

    public List<String> getOwnerNamesByHouseId(String houseId) throws Exception {
        return houseDao.getOwnerNamesByHouseId(houseId);
    }

    // 房屋CRUD
    public void addHouse(House house) throws Exception {
        houseDao.addHouse(house);
    }

    public void updateHouse(House house) throws Exception {
        houseDao.updateHouse(house);
    }

    public void deleteHouse(String id) throws Exception {
        houseDao.deleteHouse(id);
    }

    public House getHouseById(String id) throws Exception {
        return houseDao.getHouseById(id);
    }

    public List<House> getAllHouses() throws Exception {
        return houseDao.getAllHouses();
    }

    public void addPropertyFee(PropertyFee fee) throws Exception {
        propertyFeeDao.addPropertyFee(fee);
    }

    public List<PropertyFee> getAllPropertyFees() throws Exception {
        return propertyFeeDao.getAllPropertyFees();
    }

    public void addParkingSpace(ParkingSpace ps) throws Exception {
        parkingSpaceDao.addParkingSpace(ps);
    }

    public List<ParkingSpace> getAllParkingSpaces() throws Exception {
        return parkingSpaceDao.getAllParkingSpaces();
    }

    // 物业费计算：假设每平米2元
    public double calculatePropertyFee(double area) {
        return area * 2.0;
    }

    // 售出停车位管理费（按年）：假设每年500元
    public double calculateSoldParkingFee() {
        return 500.0;
    }

    // 租用停车位租金（按月）：假设每月200元
    public double calculateRentedParkingFee() {
        return 200.0;
    }
}
